<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Admin\Item;
use App\Admin\Item_img;
use App\Admin\Item_attribute;
use App\Admin\Item_stock;
use App\Admin\Classification;
use App\Admin\Attribute;
use App\Admin\Item_graphicAndText;
use Input;
use zgldh\QiniuStorage\QiniuStorage;

class ItemController extends Controller
{
    public function index(){
        $data = Item::get();
        $class = Classification::select('id','classification_name') -> get();
        return view('admin.item.index',['data' => $data,'class' => $class]);
    }

    public function detail(){
        $id = Input::get('id');
        $res['img_list'] = Item_img::where('item_id',$id) -> pluck('img');
        $stock = Item_stock::where('item_id',$id) -> get() -> toArray();
        $attr_ids = '';
        foreach($stock as $k => $v){
            $attr_ids .= $v['item_attribute_ids'] . ',';
            $stock[$k]['item_attribute_ids'] = explode(',',$v['item_attribute_ids']);
        }
        unset($k,$v);
        $attr_ids = array_unique(explode(',',rtrim($attr_ids,',')));
        $attr_values = Item_attribute::whereIn('id',$attr_ids) -> pluck('attribute_value','id');
        foreach($stock as $k => $v){
            foreach($v['item_attribute_ids'] as $lk => $lv){
                $stock[$k]['item_attribute_ids'][$lk] = $attr_values[$lv];
            }
        }
        $res['stock'] = $stock;

        if($res){
            $data = [
                'status' => 200,
                'msg'    => 'success',
                'info'   => $res,
            ];
        }else{
            $data = [
                'status' => 0,
                'msg'    => '无数据'
            ];
        }
        return response() -> json($data);
    }

    public function add(){

    }

    public function edit(){
        if(Input::method() == 'POST'){
            $data   = Input::all();

            $url = Item::where('id',$data['id']) -> value('cover_img');
            if($url != $data['cover_img']){
                //删除原有图片
                $disk = QiniuStorage::disk('qiniu_item');
                if($disk->exists($url) == true){
                    $disk->delete($url);
                }
            }

            //修改数据
            $result = Item::where('id',$data['id']) -> update([
                'classification_id' => $data['classification_id'],
                'item_name'         => $data['item_name'] ? $data['item_name'] : '',
                'item_price'        => $data['item_price'] ? $data['item_price'] : 0.00,
                'item_mprice'       => $data['item_mprice'] ? $data['item_mprice'] : 0.00,
                'item_cprice'       => $data['item_cprice'] ? $data['item_cprice'] : 0.00,
                'cover_img'         => $data['cover_img'] ? $data['cover_img'] : '',
                'stock'             => $data['stock'] ? $data['stock'] : 0,
                'sort'              => $data['sort'] ? $data['sort'] : 0,
                'status'            => $data['status'] ? $data['status'] : 2,
            ]);
            session(['cover_img_url' => null]);
            return $result ? '1' : '0';
        }else{
            $id       = Input::get('id');
            $data     = Item::where('id',$id) -> first();
            $Img_list = Item_img::where('item_id',$id) -> get();
            $class    = Classification::get();
            $stock    = Item_stock::where('item_id',$id) -> get() -> toArray();
            if(isset($stock)){
                $attr_ids = '';
                foreach($stock as $k => $v){
                    $attr_ids .= $v['item_attribute_ids'] . ',';
                    $stock[$k]['item_attribute_ids'] = explode(',',$v['item_attribute_ids']);
                }
                unset($k,$v);
                $attr_ids = array_unique(explode(',',rtrim($attr_ids,',')));
                $attr_values = Item_attribute::whereIn('id',$attr_ids) -> pluck('attribute_value','id');
                foreach($stock as $k => $v){
                    foreach($v['item_attribute_ids'] as $lk => $lv){
                        $stock[$k]['item_attribute_ids'][$lk] = $attr_values[$lv];
                    }
                }
                unset($k,$v);
                $item_attribute = Item_attribute::where('item_id',$id) -> get();
                $array_attr = [];
                foreach($item_attribute as $k => $v){
                    $array_attr[$v['attribute_id']][$k]['id']= $v['id'];
                    $array_attr[$v['attribute_id']][$k]['value']= $v['attribute_value'];
                }
                unset($k,$v);
            }else{
                $item_attribute = [];
                $array_attr = [];
            }

            $attribute = Attribute::get();
            foreach($attribute as $k => $v){
                $attribute[$k]['attribute_values'] = explode(',',$v['attribute_values']);
            }
            unset($k,$v);

            $infoText = Item_graphicAndText::where('item_id',$id) -> get();
            return view('admin.item.edit',['data' => $data ,'Img_list' => $Img_list,'class' => $class,'stock' => $stock,'item_attribute' => $item_attribute,'attribute' => $attribute,'array_attr' => $array_attr,'infoText' => $infoText]);
        }
    }

    public function del(){

    }

    public function search(){
        $class      = Classification::select('id','classification_name') -> get();
        $keyword    = Input::get('key');
        $start_time = Input::get('datemin') ? Input::get('datemin') . ' 00:00:00' : '1999-01-01 00:00:00';
        $end_time   = Input::get('datemax') ? Input::get('datemax') . ' 23:59:59' : date('Y-m-d H:i:s');
        $field      = Input::get('shaixuan') == 1 ? 'is_sowing_map' : (Input::get('shaixuan') == 2 ? 'is_home_show' : 'id');
        $eqOrneq    = Input::get('shaixuan') == 1 || Input::get('shaixuan') == 2 ? '=' : '<>';
        $field1     = Input::get('class_id') != 0 ? 'classification_id' : 'id';
        $eqOrneq1   = Input::get('class_id') != 0 ? '=' : '<>';
        $value      = Input::get('shaixuan') == 1 || Input::get('shaixuan') == 2 ? '1' : '';
        $value1     = Input::get('class_id') != 0 ? Input::get('class_id') : '';
        $data = Item::where('item_name','LIKE',"%$keyword%") -> whereBetween('created_at',[$start_time,$end_time]) -> where($field,$eqOrneq,$value) -> where($field1,$eqOrneq1,$value1) -> get();
        //展示视图，携带数据
        return view('admin.item.index',['data' => $data, 'key' => $keyword ,'datemin' => Input::get('datemin') , 'datemax' => Input::get('datemax'),'shaixuan' => Input::get('shaixuan'),'class' => $class,'class_id' => Input::get('class_id')]);
    }
}